package com.rocbrid.order.controller;


import com.rocbrid.order.bean.Order;
import com.rocbrid.order.properties.OrderProperties;
import com.rocbrid.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RefreshScope//配置自动刷新
@RestController
//@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Value("${order.timeout}")
    String orderTimeout;
    @Value("${order.auto-confirm}")
    String orderAutoConfirm;

    /**
     * 配置中心进阶方式二用配置类来实现
     * @return
     */
    @Autowired
    OrderProperties orderProperties;
    @GetMapping("/configtow")
    public String configTwo(){
        return "order.timeout="+orderProperties.getTimeout()+":       order.auto-confirm="+orderProperties.getAutoConfirm();
    }

    /**
     * 配置中心方式一
     * @return
     */
    @GetMapping("/config")
    public String config(){
        return "order.timeout="+orderTimeout+",order.auto-confirm="+orderAutoConfirm;
    }

    //创建订单,远程调用接口
    //http://localhost:8001/create?userId=1&productId=1
    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId) {
        Order order = orderService.createOrder(userId, productId);

        return order;
    }

    @GetMapping("/readDb")
    public  String readDb(){
        log.info("readDb...");
        return "readDb sucess.....";
    }

}
